package org.mspring.mlog.web.module.web.ucenter.widget;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Post;
import org.mspring.mlog.entity.PostCatalog;
import org.mspring.mlog.service.PostCatalogService;
import org.mspring.mlog.service.PostService;
import org.mspring.mlog.service.security.UserService;
import org.mspring.mlog.web.module.web.ucenter.UCenter_AbstractWidget;
import org.mspring.platform.utils.RequestUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 用于UCenter页面的相关Widget
 * 
 * @author Gao Youbo
 * @since 2013年7月4日
 */
@Widget
@RequestMapping("/u/{username}/widget")
public class UCenterWidget_CommonWidget extends UCenter_AbstractWidget {
    @Autowired
    private UserService userService;
    @Autowired
    private PostCatalogService postCatalogService;
    @Autowired
    private PostService postService;

    /**
     * 分类列表
     * 
     * @param user
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/catalog/list")
    public String catalogList(HttpServletRequest request, HttpServletResponse response, Model model) {
        List<PostCatalog> postCatalogs = postCatalogService.findPostCatalogByUser(spaceUser.getId());
        model.addAttribute("postCatalogs", postCatalogs);
        return "skin:/ucenter/widget/catalog/list";
    }

    /**
     * 最近发表的文章
     * 
     * @param user
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/post/recent")
    public String recentPost(HttpServletRequest request, HttpServletResponse response, Model model) {
        int num = RequestUtils.getRequestParameterAsInteger(request, "num");
        List<Post> posts = postService.recent(spaceUser.getId(), num);
        model.addAttribute("posts", posts);
        return "skin:/ucenter/widget/post/recent";
    }

    /**
     * 热门文章（点击率最高的文章）
     * 
     * @param user
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/post/hot")
    public String hotPost(HttpServletRequest request, HttpServletResponse response, Model model) {
        int num = RequestUtils.getRequestParameterAsInteger(request, "num");
        Date start = RequestUtils.getRequestParameterAsDate(request, "start");
        Date end = RequestUtils.getRequestParameterAsDate(request, "end");
        List<Post> posts = postService.hot(spaceUser.getId(), num, start, end);
        model.addAttribute("posts", posts);
        return "skin:/ucenter/widget/post/hot";
    }

    /**
     * 置顶文章
     * 
     * @param user
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/post/top")
    public String topPost(HttpServletRequest request, HttpServletResponse response, Model model) {
        int num = RequestUtils.getRequestParameterAsInteger(request, "num");
        List<Post> posts = postService.top(spaceUser.getId(), num);
        model.addAttribute("posts", posts);
        return "skin:/ucenter/widget/post/top";
    }
}
